<template>
  <div class="modal" :class="{ active: isShow }">
      <div class="mask" @click="clickMask"></div>
      <div class="inner">
        <div class="content">{{ message }}</div>
        <div class="modal-footer">
          <div @click="clickCancel">{{ config.cancelText || '取消' }}</div>
          <div class="right" @click="clickConfirm">{{ config.confirmText || '确定' }}</div>
        </div>
      </div>
    </div>
</template>

<script>
/**@augments
 * message - text
 * callback - Function
 * config - Object
 *   - confirmText - text
 *   - cancelText - text
 *   - maskEvent - boolean - whether trigger cancel event when clicking mask
 */
export default {
  props: {
    message: String,
    callback: Function,
    config: { type: Object, default: () => ({}) }
  },
  data () {
    return {
      isShow: false,
    }
  },
  created () {
  },
  mounted () {
    requestAnimationFrame(() => {
      this.isShow = true
    })
  },
  methods: {
    clickCancel () {
      this.callback && this.callback(false)
      this.destroy()
    },
    clickConfirm () {
      this.callback && this.callback(true)
      this.destroy()
    },
    clickMask () {
      if (this.config.maskEvent) {
        this.clickCancel()
      }
    },
    destroy () {
      this.isShow = false
      setTimeout(() => {
        this.$destroy()
        this.$el.parentElement.removeChild(this.$el)
      }, 300);
    }
  }
}
</script>

<style scoped>
.modal {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.modal.active {
  pointer-events: auto;
}
.modal .mask {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.6);
  opacity: 0;
  transition: all 0.3s ease-in;
}
.modal.active .mask {
  opacity: 1;
}
.modal .inner {
  min-width: 260px;
  max-width: 340px;
  max-height: 80%;
  background-color: #fff;
  position: relative;
  z-index: 10;
  border-radius: 5px;
  transform: translateY(-30px);
  transition: all 0.3s ease-in;
  opacity: 0;
  overflow: hidden;
}
.modal.active .inner {
  transform: translateY(0);
  opacity: 1;
}
.modal .inner {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.modal .inner .content {
  flex: auto;
  overflow-y: auto;
  width: 100%;
  padding: 40px;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  box-sizing: border-box;
  text-align: center;
  word-break: break-all;
}
.modal .inner .modal-footer {
  display: flex;
  width: 100%;
}
.modal .inner .modal-footer div {
  flex: auto;
  padding: 8px 15px;
  text-align: center;
  cursor: pointer;
  font-size: 14px;
}
.modal .inner .modal-footer div:hover {
  background-color: rgba(0, 0, 0, 0.2);
}
.modal .inner .modal-footer div.right {
  border-left: 1px solid #ccc;
  background-color: #25a5e0;
  color: #fff;
}
</style>
